"""
    Author:
    Date: 2019-10-26

    房间品类管理类
"""

from entities.consts import *
from utils import db


def get_room_tag_list():
    """ 获取房间品类列表 """
    sql = f'''
        SELECT      *
        FROM        cmf_config_room_tags
        ORDER BY    show_in_homepage DESC,
                    show_in_chat_room_page DESC,
                    orderno,
                    id;
    '''
    data = db.query_data_from_db(sql)
    return data if data else []


def get_room_tag_list_in_homepage():
    """ 获取首页房间品类列表 """
    sql = f'''
        SELECT      *
        FROM        cmf_config_room_tags
        ORDER BY    show_in_homepage DESC,
                    orderno,
                    id;
    '''
    data = db.query_data_from_db(sql)
    return data if data else []


def get_room_tag_list_in_chat_room_page():
    """ 获取聊天室房间品类列表 """
    sql = f'''
        SELECT      *
        FROM        cmf_config_room_tags
        ORDER BY    show_in_chat_room_page DESC,
                    orderno,
                    id;
    '''
    data = db.query_data_from_db(sql)
    return data if data else []


def get_room_count_group_by_tag():
    """ 根据房间品类获取房间数量 """
    sql = f'''
        SELECT      room_tag, COUNT(*) AS room_count
        FROM        cmf_users_auth
        WHERE       status={AUTH_STATUS_CERTIFIED}
        GROUP BY    room_tag;
    '''
    data = db.query_data_from_db(sql)
    if data is None:
        return {}

    rooms_count = {}
    for item in data:
        key = item['room_tag']
        value = item['room_count']
        rooms_count[key] = value
    return rooms_count


def enable_room_tag_in_homepage(room_tag_id):
    """ 启用首页发房间品类 """
    sql = f'''
        UPDATE      cmf_config_room_tags
        SET         show_in_homepage=1
        WHERE       tag_id={room_tag_id} AND
                    show_in_homepage=0;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def disable_room_tag_in_homepage(room_tag_id):
    """ 禁用首页发房间品类 """
    sql = f'''
        UPDATE      cmf_config_room_tags
        SET         show_in_homepage=0
        WHERE       tag_id={room_tag_id} AND
                    show_in_homepage=1;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def enable_room_tag_in_chat_room_page(room_tag_id):
    """ 启用聊天室发房间品类 """
    sql = f'''
        UPDATE      cmf_config_room_tags
        SET         show_in_chat_room_page=1
        WHERE       tag_id={room_tag_id} AND
                    show_in_chat_room_page=0;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def disable_room_tag_in_chat_room_page(room_tag_id):
    """ 禁用聊天室发房间品类 """
    sql = f'''
        UPDATE      cmf_config_room_tags
        SET         show_in_chat_room_page=0
        WHERE       tag_id={room_tag_id} AND
                    show_in_chat_room_page=1;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def get_max_tag_id():
    """ 获取最大的房间分类列表 """
    sql = '''
        SELECT    MAX(tag_id) AS max_tag_id
        FROM      cmf_config_room_tags;
    '''
    data = db.query_data_from_db(sql)
    return data[0]['max_tag_id'] if data else 0


def get_room_tag(tag_id):
    """ 根据id获取房间品类 """
    sql = f'''
        SELECT      *
        FROM        cmf_config_room_tags
        WHERE       tag_id={tag_id};
    '''
    data = db.query_data_from_db(sql)
    return data[0] if data else None


def new_room_tag(tag_id, tag_name, heat_factor, orderno):
    """ 添加新的房间品类 """
    sql = '''
        INSERT INTO cmf_config_room_tags
        SET         tag_id=%(tag_id)s,
                    tag_name=%(tag_name)s,
                    heat_factor=%(heat_factor)s,
                    orderno=%(orderno)s,
                    show_in_homepage=0,
                    show_in_chat_room_page=0
    '''
    params = {
        'tag_id': tag_id,
        'tag_name': tag_name,
        'heat_factor': str(heat_factor),
        'orderno': str(orderno),
    }
    affected = db.execute_sql(sql, params)
    return affected == 1


def edit_room_tag(tag_id, tag_name, heat_factor, orderno):
    """ 编辑房间品类 """
    sql = f'''
        UPDATE      cmf_config_room_tags
        SET         tag_id=%(tag_id)s,
                    tag_name=%(tag_name)s,
                    heat_factor=%(heat_factor)s,
                    orderno=%(orderno)s
        WHERE       id=%(tag_id)s;
    '''
    params = {
        'tag_id': str(tag_id),
        'tag_name': tag_name,
        'heat_factor': str(heat_factor),
        'orderno': str(orderno),
    }
    affected_rows = db.execute_sql(sql, params)
    return affected_rows > 0


if __name__ == '__main__':
    print('ok')
